package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static javax.print.attribute.standard.MediaSizeName.C;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public List<CategoryDTO> findCategoryById(List<Long> id) {
        List<Category> categories = baseMapper.selectBatchIds(id);
        return CategoryDTO.convertEntityList(categories);
    }

    @Override
    public List<CategoryDTO> findCategoryByPid(Long id) {
        //定义条件构造器
      /*  List<Category> parent_id = query().eq("parent_id", id).list();
        return CategoryDTO.convertEntityList(parent_id);*/
      QueryWrapper<Category> queryWrapper = new QueryWrapper();
      queryWrapper.eq("parent_id", id);
        List<Category> categories = baseMapper.selectList(queryWrapper);
        return CategoryDTO.convertEntityList(categories);
    }

    @Override
    public List<CategoryDTO> findCategoryByBid(Long bid) {
        List<Long> categoryByBid = baseMapper.findCategoryByBid(bid);
        return findCategoryById(categoryByBid);
        //select * from tb_category_brand where brand_id = #{bid}
      /*  List<CategoryBrand> categoryBrands =
                this.categoryBrandService
                        .query()
                        .eq("brand_id", bid)
                        .list();

        //从分类品牌的集合中获取所有的分类id
        List<Long> cids = categoryBrands
                .stream()
                .map(CategoryBrand::getCategoryId)
                .collect(Collectors.toList());

        return findCategoryById(cids);*/
    }
}
